package com.maistars.spider.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maistars.spider.common.constants.PageType;
import com.maistars.spider.common.constants.RenderEngineType;
import com.maistars.spider.common.exception.TopicSpiderException;
import com.maistars.spider.repository.dao.CrawlerTaskDao;
import com.maistars.spider.repository.model.CrawlerFieldRulePO;
import com.maistars.spider.repository.model.CrawlerPageRulePO;
import com.maistars.spider.service.domain.render.PageResponse;
import com.maistars.spider.infrastructure.adapter.SeleniumAdapter;
import com.maistars.spider.service.domain.render.PageRequest;
import com.maistars.spider.service.render.RenderService;
import com.maistars.spider.view.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.CookieStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理爬虫相关内容
 *
 * @author dhr
 * @date 2020/10/4 5:30 下午
 */
@Slf4j
@Controller
public class ExtractorController extends BaseController {

    @Autowired
    private CrawlerTaskDao crawlerTaskDao;

    private RenderService renderService = new RenderService();

    @GetMapping(value = {"/extractor/xpath"})
    public ModelAndView xpathExtractor(Model model, HttpServletRequest request) throws TopicSpiderException {
        String pageRuleId = request.getParameter("pageRuleId");

        CrawlerPageRulePO crawlerPageRulePO = crawlerTaskDao.getCrawlerPageRule(Integer.parseInt(pageRuleId));
        Integer taskId = crawlerPageRulePO.getTaskId();
        Integer pageType = crawlerPageRulePO.getPageType();

        Map<String, String> extractorParams = new HashMap<>();
        extractorParams.put("pageType", String.valueOf(pageType));
        extractorParams.put("taskId", String.valueOf(taskId));
        extractorParams.put("pageRuleId", pageRuleId);
        extractorParams.put("renderConfig", crawlerPageRulePO.getRenderConfig() == null ? "{}" : crawlerPageRulePO.getRenderConfig());
        extractorParams.put("pageTurnRule", crawlerPageRulePO.getPageTurnRule() == null ? "{}" : crawlerPageRulePO.getPageTurnRule());

        String urlRule = crawlerPageRulePO.getUrlRule();
        String sampleUrl = "";
        if (urlRule != null) {
            JSONObject jsonObject = JSONObject.parseObject(urlRule);
            sampleUrl = jsonObject.getString("sampleUrl") == null ? "" : jsonObject.getString("sampleUrl");
        }
        extractorParams.put("sampleUrl", sampleUrl);

        JSONObject urlObject = JSONObject.parseObject(crawlerPageRulePO.getUrlRule());
        Integer parseType = urlObject.getInteger("parseType");
        extractorParams.put("parseType", String.valueOf(parseType));

        model.addAttribute("extractorParams", extractorParams);
        return new ModelAndView("/tools/XpathExtractor");
    }

    @RequestMapping("/extractor/xpath/loadHtml")
    @ResponseBody
    public ResponseResult<String> loadHtml(HttpServletRequest request) throws Exception {
        ResponseResult<String> responseResult = new ResponseResult<String>();
        responseResult.setCode(100);

        String url = request.getParameter("url");
        String renderConfig = request.getParameter("renderConfig");

        PageRequest pageRequest = new PageRequest();
        pageRequest.setUrl(url);

        JSONObject renderConfigObj = JSONObject.parseObject(renderConfig);

        pageRequest.getRenderConfig().setDynamic(renderConfigObj.getBoolean("isDynamic"));
        pageRequest.getRenderConfig().setMethod(renderConfigObj.getString("method"));
        pageRequest.getRenderConfig().setHtmlHandler(renderService.getDefaultHtmlHandler());
        pageRequest.getRenderConfig().setPageLoadTimeout(10);
        pageRequest.getRenderConfig().setScriptLoadTimeout(30);
        pageRequest.getRenderConfig().getExtraParams().put("chromeDriverPath", globalConfig.getChromeDriverPath());

//        initCookie(request, pageRequest);

        PageResponse pageResponse = renderService.renderPage(RenderEngineType.SIMPLE, pageRequest);
        responseResult.setData(pageResponse.getRenderResult());
        return responseResult;
    }

    private void initCookie(HttpServletRequest request, PageRequest pageRequest) throws Exception {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        CookieStore cookieStore = SeleniumAdapter.login(globalConfig.getChromeDriverPath(), "https://huaban.com/");
        Map<String, String> cookie = new HashMap<>();

        cookieStore.getCookies().forEach(cookieEle -> {
            pageRequest.getRenderConfig().getCookies().put(cookieEle.getName(), cookieEle);
        });
    }

}
